namespace Mytodotodaylist.Core.Events
{
    using System;
    using System.Collections.Generic;
    using Container;

    public class BuildEventsFromRawSourceSpec
    {
        public DateTime DateTimeNow;
        public DateTime DateTimeEndOfDay;

        private readonly IEventRepository EventRepository;

        public BuildEventsFromRawSourceSpec(IEventRepository eventRepository, DateTime dateTimeNow)
        {
            EventRepository = eventRepository;
            DateTimeNow = dateTimeNow;
        }

        public BuildEventsFromRawSourceSpec(): this(IoC.Resolve<IEventRepository>(), DateTime.Now)
        {
        }

        public List<Event> Build()
        {
            DateTimeEndOfDay = new DateTime(
                DateTimeNow.Year,
                DateTimeNow.Month,
                DateTimeNow.Day, 
                23,
                59,
                59);

            var result = new List<Event>();
            var events = EventRepository.GetAll();

            var now = DateTime.Now;

            // process
            events.ForEach(x=>
                               {
                                   if (x.Frequency == Periodicity.None)
                                       result.Add(x);
                                   
                                   if (x.Frequency == Periodicity.OnDate)
                                       result.Add(x);

                                   if(x.Frequency == Periodicity.EveryDay)
                                   {
                                       x.Date = new DateTime(
                                           now.Year, 
                                           now.Month, 
                                           now.Day, 
                                           x.Date.Hour, 
                                           x.Date.Minute, 
                                           x.Date.Second, 
                                           x.Date.Millisecond);
                                       result.Add(x);
                                   }

                                   if (x.Frequency == Periodicity.Scheduled)
                                   {
                                       int scheduledInterval = x.Date.Minute + x.Date.Hour * 60;

                                       DateTime startTime = DateTimeNow;

                                       while (startTime < DateTimeEndOfDay)
                                       {
                                           var theNewEvent = new Event
                                                                 {
                                                                     Id= x.Id,
                                                                     Name = x.Name,
                                                                     Frequency = x.Frequency,
                                                                     Date = startTime
                                                                 };

                                           result.Add(theNewEvent);
                                           startTime = startTime.AddMinutes(scheduledInterval);
                                       }
 
                                   }
                               });

            return result;
        }
    }
}